<template>
  <view class="city">

    <!-- 当前定位城市 -->
    <view class="city-title xs">当前定位城市</view>
    <view class="city-current bg-white flex row-between">
      <view>
        <u-icon name="map-fill" size="34"></u-icon>
        <text class="m-l-8 nr" v-if="!isLoading">{{ cityInfo.name || '城市' }}</text>
        <text class="m-l-8 nr" v-else>定位中...</text>
      </view>
      <view class="reselect nr flex" @click="reLocationFunc">
        <u-loading mode="flower" color="#528FFF" size="28" v-if="isLoading"></u-loading>
        <text class="m-l-8">重新定位</text>
      </view>
    </view>

    <!-- 历史浏览城市 -->
    <view class="city-title xs" v-if="historyList.length">历史浏览城市</view>
    <view class="city-list bg-white">
      <block v-for="(hisItem, hisIndex) in historyList" :key="hisIndex">
        <view class="city-list-item line-1 nr" @click="chooseCity(hisItem)">{{ hisItem.name }}</view>
      </block>
    </view>
    <!-- 普通城市列表 -->
    <view >
      <view class="city-list bg-white">
        <block v-for="(cityItem2, cityIndex2) in cityList" :key="cityIndex2">
          <view class="city-list-item line-1 nr" @click="chooseCity(cityItem2)">
            {{ cityItem2.name }}
          </view>
        </block>
      </view>
    </view>
    <!-- 侧边弹窗 -->
    <view class="city-list-alert" v-if="touchmove && labelList[touchmoveIndex+'']">
      <text>{{labelList[touchmoveIndex]}}</text>
    </view>
  </view>
</template>

<script>
import { mapActions, mapMutations, mapGetters } from 'vuex'
import { currentPage, toast } from "@/utils/tools.js"

export default {
  data() {
    return {
      isLoading: false,
      cityList: [],
      labelList: [],
      historyList: [],
      hotList: [
      ],
      isFirstLoading: true,

      touchmove: false,
      touchmoveIndex: 0,
    }
  },
  onLoad() {
    this.getCityListsFunc()
  },
  onPageScroll({ scrollTop }) {
    const anchor = this.anchor;
    const index = anchor.findIndex(item => item >= scrollTop);
    const isLessTop = index !== -1;
    if (isLessTop && !this.touchmmove) this.touchmoveIndex = index;
  },
  methods: {
    ...mapActions(['initLocationFunc']),
    ...mapMutations(['setCityInfo']),
    async reLocationFunc() {
      this.isLoading = true;
      await this.initLocationFunc()
      this.isLoading = false;
    },
    chooseCity(city) {
      this.setCityInfo(city);
     // 返回上一页
      uni.navigateBack({
        delta: 1
      });
    },

    getCityListsFunc() {
      this.$api.getCityList({
        pid: 0
      })
          .then(res => {
            this.cityList = res;
          })
    },
  },
  computed: {
    ...mapGetters(['cityInfo'])
  }
}
</script>

<style lang="scss">
.city {
  padding: 12rpx 0;

  &-title {
    color: #AAAAAA;
    padding: 12rpx 34rpx;
  }

  .city-current {
    color: #575757;
    padding: 0 36rpx;

    .reselect {
      color: #528FFF;
      padding: 26rpx 10rpx;
    }
  }

  &-list {
    color: #575757;
    padding: 0 40rpx;
    display: flex;
    flex-wrap: wrap;

    &-item {
      width: 25%;
      height: 96rpx;
      line-height: 96rpx;
      padding-left: 34rpx;
    }
  }

  &-bar__sidebar {
    position: fixed;
    top: 50%;
    right: 0;
    display: flex;
    flex-direction: column;
    text-align: center;
    transform: translateY(-50%);
    user-select: none;
    z-index: 99;
  }

  &-bar__index {
    font-weight: 500;
    padding: 8rpx 18rpx;
    font-size: 22rpx;
    line-height: 1
  }

  &-list-alert {
    position: fixed;
    width: 120rpx;
    height: 120rpx;
    right: 90rpx;
    top: 50%;
    margin-top: -60rpx;
    border-radius: 24rpx;
    font-size: 50rpx;
    color: #fff;
    background-color: rgba(0, 0, 0, 0.65);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0;
    z-index: 9999999;

    text {
      line-height: 50rpx;
    }
  }
}
</style>
